const { task } = require('folktale/concurrency/task')
const { split, find } = require('lodash/fp')
const fs = require('fs')
// task

function readFile(filename) {
  // 返回task函子 需要接受一个函数resolver 包括reject和resolve方法
  return task((resolver) => {
    // readFile异步读取文件 node错误优先
    fs.readFile(filename, 'utf-8', (err, data) => {
      // 如果出错
      if (err) resolver.reject(err)
      // 成功
      resolver.resolve(data)
    })
  })
}

readFile('package.json')
  // 换行 切割数组
  .map(split('\n'))
  .map(find((x) => x.includes('version')))
  .run()
  .listen({
    onRejected: (err) => {
      console.log(err)
    },
    onResolved: (value) => {
      console.log(value)
    },
  })
// "version": "1.0.0",
